package com.rui.responsibility.model;

import com.rui.responsibility.obj.User;

/**
 * @Description: handler抽象类
 * @Author: ZhangRui
 * @Date: 2023/3/3 15:03
 * @Version: 1.0
 */
public abstract class Handler<T> {

    protected Handler next;

    /**
     * 返回handler方便链式操作
     *
     * @param null
     * @return
     * @author ZhangRui
     * @date 2023/3/3 15:10
     */
    public Handler next(Handler next) {
        this.next = next;
        return next;
    }

    /**
     * 流程开始的方法
     *
     * @param null
     * @return
     * @author ZhangRui
     * @date 2023/3/3 15:10
     */
    public abstract void doHandler(User user);

    public static class Builder<T> {

        private Handler<T> head;
        private Handler<T> tail;

        public Builder<T> addHandler(Handler<T> handler) {
            if (this.head == null) {
                this.head = this.tail = handler;
                return this;
            }
            this.tail.next(handler);
            this.tail = handler;
            return this;
        }

        public Handler<T> build() {
            return this.head;
        }
    }
}
